﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Lite.Core.Office;
using P = DocumentFormat.OpenXml.Lite.PresentationML.Presentation.Base;
using DocumentFormat.OpenXml.Lite.PresentationML.Presentation.PresentationElements;
using DocumentFormat.OpenXml.Lite.PresentationML.Presentation.Types;
using DocumentFormat.OpenXml.Lite.PresentationML.Presentation.SlideElements;
using DocumentFormat.OpenXml.Lite.PresentationML.Drawing;
using DMLC = DocumentFormat.OpenXml.Lite.DrawingML.Color;
using DMLE = DocumentFormat.OpenXml.Lite.DrawingML.Effect;
using DMLF = DocumentFormat.OpenXml.Lite.DrawingML.Font;
using DMLG = DocumentFormat.OpenXml.Lite.DrawingML.Geometry;
using DMLGP = DocumentFormat.OpenXml.Lite.DrawingML.Geometry.Properties;
using DMLTx = DocumentFormat.OpenXml.Lite.DrawingML.Text;
using DMLTf = DocumentFormat.OpenXml.Lite.DrawingML.Transformation;
using DMLT = DocumentFormat.OpenXml.Lite.DrawingML.Types;
using DMLS = DocumentFormat.OpenXml.Lite.DrawingML.Style;
using DMLV = DocumentFormat.OpenXml.Lite.DrawingML.Visual;

namespace Samples.Presentation
{
    class Shapes
    {
        public static void Create()
        {
            P.Presentation pres = DocumentManager.Instance.CreatePresentation();
            pres.SlideSize = new SlideSize()
            {
                ExtentLength = 9144000,
                ExtentWidth = 6858000,
                Type = ST_SlideSize.Screen_4x3
            };
            pres.NoteSize = new NoteSize()
            {
                ExtentLength = 6858000,
                ExtentWidth = 9144000
            };

            P.SlideMaster master = pres.CreateSlideMaster();
            P.SlideLayout layout = pres.CreateLayout(master);
            P.Slide slide = pres.CreateSlide(layout);
            DMLS.Theme theme = pres.CreateTheme(master);

            #region Create Master

            {
                master.SlideData = new CommonSlideData()
                {
                    Name = "Title Slide",
                    ShapeTree = new ShapeTree()
                    {
                        NonVisualGroupShapeProperties = new NonVisualGroupShapeProperties()
                        {
                            NonVisualDrawingProperties = new DMLGP.NonVisualProperties()
                            {
                                Id = 1,
                                Name = ""
                            },
                            NonVisualGroupShapeDrawingProperties = new DMLGP.NonVisualGroupShapeDrawingProperties(),
                            NonVisualProperties = new NonVisualProperties()
                        },
                        GroupShapeProperties = new DMLGP.GroupShapeProperties()
                        {
                            Transform2d = new DMLTf.GroupTransform2d()
                            {
                                Offset = new DMLG.Offset() { X = 0, Y = 0 },
                                Extents = new DMLG.Extent() { ExtentLength = 0, ExtentWidth = 0 },
                                ChildOffset = new DMLG.Offset() { X = 0, Y = 0 },
                                ChildExtents = new DMLG.Extent() { ExtentLength = 0, ExtentWidth = 0 }
                            }
                        }
                    }
                };

                #region Shape1

                Shape shape1;
                master.SlideData.ShapeTree.Shapes.Add(shape1 = new Shape()
                {
                    NonVisualProperties = new NonVisualShapeProperties()
                    {
                        NonVisualDrawingProperties = new DMLGP.NonVisualProperties()
                        {
                            Id = 2,
                            Name = "Title"
                        },
                        NonVisualShapeDrawingProperties = new DMLGP.NonVisualShapeDrawingProperties()
                        {
                            ShapeLocks = new DMLG.Locks.ShapeLocks() { DisallowGrouping = true }
                        },
                        NonVisualProperties = new NonVisualProperties()
                        {
                            Placeholder = new PlaceholderShape() { Type = ST_PlaceholderType.Title }
                        }
                    },
                    Properties = new DMLGP.ShapeProperties()
                    {
                        Transform2d = new DMLTf.Transform2d()
                        {
                            Offset = new DMLG.Offset() { X = 457200, Y = 274638 },
                            Extents = new DMLG.Extent() { ExtentLength = 8229600, ExtentWidth = 1143000 }
                        },
                        Geometry = new DMLTf.PresetGeometry()
                        {
                            ShapeType = DMLT.ST_ShapeType.Rectangle,
                            AdjustValues = new DMLG.AdjustValueList()
                        }
                    },
                    Body = new DMLTx.Body()
                    {
                        Properties = new DMLTx.Properties.BodyProperties()
                        {
                            VerticalText = DMLT.ST_TextVerticalType.Horizontal,
                            LeftInset = 91440,
                            TopInset = 45720,
                            RightInset = 91440,
                            BottomInset = 45720,
                            ColumnsRightToLeft = false,
                            Anchor = DMLT.ST_TextAnchoringType.Center,
                            AutoFit = new DMLS.NormalAutofit()
                        },
                        Styles = new DMLTx.Properties.TextListStyles()
                    }
                });

                DMLTx.Paragraph p1;
                shape1.Body.Paragraphs.Add(p1 = new DMLTx.Paragraph());
                p1.Elements.Add(new DMLTx.Run()
                {
                    Text = new DocumentFormat.OpenXml.Lite.Shared.SimpleText() { Value = "Title" }
                });

                #endregion

                #region Shape2

                Shape shape2;
                master.SlideData.ShapeTree.Shapes.Add(shape2 = new Shape()
                {
                    NonVisualProperties = new NonVisualShapeProperties()
                    {
                        NonVisualDrawingProperties = new DMLGP.NonVisualProperties()
                        {
                            Id = 3,
                            Name = "Subtitle"
                        },
                        NonVisualShapeDrawingProperties = new DMLGP.NonVisualShapeDrawingProperties()
                        {
                            ShapeLocks = new DMLG.Locks.ShapeLocks() { DisallowGrouping = true }
                        },
                        NonVisualProperties = new NonVisualProperties()
                        {
                            Placeholder = new PlaceholderShape() { Type = ST_PlaceholderType.Body, Index = 1 }
                        }
                    },
                    Properties = new DMLGP.ShapeProperties()
                    {
                        Transform2d = new DMLTf.Transform2d()
                        {
                            Offset = new DMLG.Offset() { X = 457200, Y = 1600200 },
                            Extents = new DMLG.Extent() { ExtentLength = 8229600, ExtentWidth = 4525963 }
                        },
                        Geometry = new DMLTf.PresetGeometry()
                        {
                            ShapeType = DMLT.ST_ShapeType.Rectangle,
                            AdjustValues = new DMLG.AdjustValueList()
                        }
                    },
                    Body = new DMLTx.Body()
                    {
                        Properties = new DMLTx.Properties.BodyProperties()
                        {
                            VerticalText = DMLT.ST_TextVerticalType.Horizontal,
                            LeftInset = 91440,
                            TopInset = 45720,
                            RightInset = 91440,
                            BottomInset = 45720,
                            ColumnsRightToLeft = false,
                            AutoFit = new DMLS.NormalAutofit()
                        },
                        Styles = new DMLTx.Properties.TextListStyles()
                    }
                });

                DMLTx.Paragraph p2;
                shape2.Body.Paragraphs.Add(p2 = new DMLTx.Paragraph()
                {
                    Properties = new DMLTx.Properties.ParagraphProperties() { Level = 0 }
                });
                p2.Elements.Add(new DMLTx.Run()
                {
                    Text = new DocumentFormat.OpenXml.Lite.Shared.SimpleText() { Value = "Subtitle" }
                });

                #endregion

                master.TextStyles = new TextStyles()
                {
                    TitleStyle = new DMLTx.Properties.TextListStyles()
                    {
                        ListLevel1Style = new DMLTx.Properties.ParagraphProperties()
                        {
                            Alignment = DMLT.ST_TextAlignType.Center,
                            DefaultTabSize = 91440,
                            RightToLeft = false,
                            EastAsianLineBreak = true,
                            LatinLineBreak = false,
                            HangingPunctuation = true,
                            SpaceBefore = new DMLTx.Space() { Spacing = new DMLTx.SpacingPercent() { Value = 0 } },
                            TextBullet = new DMLTx.NoBullet(),
                            RunProperties = new DMLTx.Properties.RunProperties()
                            {
                                FontSize = 4400,
                                Kerning = 1200,
                                FillProperties = new DMLV.SolidFill()
                                {
                                    ColorElement = new DMLC.SchemeColorModel() { ColorValue = DMLT.ST_SchemeColorVal.TextColor1 }
                                }
                            }
                        },
                        ListLevel2Style = new DMLTx.Properties.ParagraphProperties()
                        {
                            Alignment = DMLT.ST_TextAlignType.Center,
                            DefaultTabSize = 91440,
                            RightToLeft = false,
                            EastAsianLineBreak = true,
                            LatinLineBreak = false,
                            HangingPunctuation = true,
                            SpaceBefore = new DMLTx.Space() { Spacing = new DMLTx.SpacingPercent() { Value = 0 } },
                            TextBullet = new DMLTx.NoBullet(),
                            RunProperties = new DMLTx.Properties.RunProperties()
                            {
                                FontSize = 2800,
                                Kerning = 1200,
                                FillProperties = new DMLV.SolidFill()
                                {
                                    ColorElement = new DMLC.SchemeColorModel() { ColorValue = DMLT.ST_SchemeColorVal.TextColor1 }
                                }
                            }
                        }
                    }
                };

                master.ColorSchemeMap = new ColorSchemeMap()
                {
                    Background1 = DMLT.ST_ColorSchemeIndex.Light1,
                    Background2 = DMLT.ST_ColorSchemeIndex.Light2,
                    Text1 = DMLT.ST_ColorSchemeIndex.Dark1,
                    Text2 = DMLT.ST_ColorSchemeIndex.Dark2,
                    Accent1 = DMLT.ST_ColorSchemeIndex.Accent1,
                    Accent2 = DMLT.ST_ColorSchemeIndex.Accent2,
                    Accent3 = DMLT.ST_ColorSchemeIndex.Accent3,
                    Accent4 = DMLT.ST_ColorSchemeIndex.Accent4,
                    Accent5 = DMLT.ST_ColorSchemeIndex.Accent5,
                    Accent6 = DMLT.ST_ColorSchemeIndex.Accent6,
                    Hyperlink = DMLT.ST_ColorSchemeIndex.Hyperlink,
                    FollowedHyperlink = DMLT.ST_ColorSchemeIndex.FollowedHyperlink
                };
            }

            #endregion

            #region Create Layout

            {
                layout.Type = ST_SlideLayout.Text;
                layout.SlideData = new CommonSlideData()
                {
                    ShapeTree = new ShapeTree()
                    {
                        NonVisualGroupShapeProperties = new NonVisualGroupShapeProperties()
                        {
                            NonVisualDrawingProperties = new DMLGP.NonVisualProperties()
                            {
                                Id = 1,
                                Name = ""
                            },
                            NonVisualGroupShapeDrawingProperties = new DMLGP.NonVisualGroupShapeDrawingProperties(),
                            NonVisualProperties = new NonVisualProperties()
                        },
                        GroupShapeProperties = new DMLGP.GroupShapeProperties()
                        {
                            Transform2d = new DMLTf.GroupTransform2d()
                            {
                                Offset = new DMLG.Offset() { X = 0, Y = 0 },
                                Extents = new DMLG.Extent() { ExtentLength = 0, ExtentWidth = 0 },
                                ChildOffset = new DMLG.Offset() { X = 0, Y = 0 },
                                ChildExtents = new DMLG.Extent() { ExtentLength = 0, ExtentWidth = 0 }
                            }
                        }
                    }
                };

                Shape shape1;
                layout.SlideData.ShapeTree.Shapes.Add(shape1 = new Shape()
                {
                    NonVisualProperties = new NonVisualShapeProperties()
                    {
                        NonVisualDrawingProperties = new DMLGP.NonVisualProperties()
                        {
                            Id = 2,
                            Name = "Title"
                        },
                        NonVisualShapeDrawingProperties = new DMLGP.NonVisualShapeDrawingProperties()
                        {
                            ShapeLocks = new DMLG.Locks.ShapeLocks() { DisallowGrouping = true }
                        },
                        NonVisualProperties = new NonVisualProperties()
                        {
                            Placeholder = new PlaceholderShape() { Type = ST_PlaceholderType.CenteredTitle }
                        }
                    },
                    Properties = new DMLGP.ShapeProperties()
                    {
                        Transform2d = new DMLTf.Transform2d()
                        {
                            Offset = new DMLG.Offset() { X = 685800, Y = 2130425 },
                            Extents = new DMLG.Extent() { ExtentLength = 7772400, ExtentWidth = 1470025 }
                        }
                    },
                    Body = new DMLTx.Body()
                    {
                        Properties = new DMLTx.Properties.BodyProperties(),
                        Styles = new DMLTx.Properties.TextListStyles()
                    }
                });

                DMLTx.Paragraph p1;
                shape1.Body.Paragraphs.Add(p1 = new DMLTx.Paragraph());
                p1.Elements.Add(new DMLTx.Run()
                {
                    Text = new DocumentFormat.OpenXml.Lite.Shared.SimpleText() { Value = "Title" }
                });

                Shape shape2;
                layout.SlideData.ShapeTree.Shapes.Add(shape2 = new Shape()
                {
                    NonVisualProperties = new NonVisualShapeProperties()
                    {
                        NonVisualDrawingProperties = new DMLGP.NonVisualProperties()
                        {
                            Id = 3,
                            Name = "Subtitle"
                        },
                        NonVisualShapeDrawingProperties = new DMLGP.NonVisualShapeDrawingProperties()
                        {
                            ShapeLocks = new DMLG.Locks.ShapeLocks() { DisallowGrouping = true }
                        },
                        NonVisualProperties = new NonVisualProperties()
                        {
                            Placeholder = new PlaceholderShape() { Type = ST_PlaceholderType.Subtitle, Index = 1 }
                        }
                    },
                    Properties = new DMLGP.ShapeProperties()
                    {
                        Transform2d = new DMLTf.Transform2d()
                        {
                            Offset = new DMLG.Offset() { X = 1371600, Y = 3886200 },
                            Extents = new DMLG.Extent() { ExtentLength = 6400800, ExtentWidth = 1752600 }
                        }
                    },
                    Body = new DMLTx.Body()
                    {
                        Properties = new DMLTx.Properties.BodyProperties(),
                        Styles = new DMLTx.Properties.TextListStyles()
                        {
                            ListLevel1Style = new DMLTx.Properties.ParagraphProperties()
                            {
                                LeftMargin = 0,
                                RightMargin = 0,
                                Alignment = DMLT.ST_TextAlignType.Center,
                                TextBullet = new DMLTx.NoBullet(),
                                RunProperties = new DMLTx.Properties.RunProperties()
                                {
                                    FillProperties = new DMLV.SolidFill()
                                    {
                                        ColorElement = new DMLC.SchemeColorModel()
                                        {
                                            ColorValue = DMLT.ST_SchemeColorVal.TextColor1
                                        }
                                    }
                                }
                            }
                        }
                    }
                });

                ((shape2.Body.Styles.ListLevel1Style.RunProperties.FillProperties as DMLV.SolidFill)
                    .ColorElement as DMLC.SchemeColorModel).ColorElements.Add(new DMLC.Tint() { Value = 75000 });

                DMLTx.Paragraph p2;
                shape2.Body.Paragraphs.Add(p2 = new DMLTx.Paragraph());
                p2.Elements.Add(new DMLTx.Run()
                {
                    Text = new DocumentFormat.OpenXml.Lite.Shared.SimpleText() { Value = "Subtitle" }
                });

                layout.Override = new ColorSchemeMapOverride() { Override = new MasterColorMapping() };
            }

            #endregion

            #region Create Slide

            {
                slide.SlideData = new CommonSlideData()
                {
                    ShapeTree = new ShapeTree()
                    {
                        NonVisualGroupShapeProperties = new NonVisualGroupShapeProperties()
                        {
                            NonVisualDrawingProperties = new DMLGP.NonVisualProperties()
                            {
                                Id = 1,
                                Name = ""
                            },
                            NonVisualGroupShapeDrawingProperties = new DMLGP.NonVisualGroupShapeDrawingProperties(),
                            NonVisualProperties = new NonVisualProperties()
                        },
                        GroupShapeProperties = new DMLGP.GroupShapeProperties()
                        {
                            Transform2d = new DMLTf.GroupTransform2d()
                            {
                                Offset = new DMLG.Offset() { X = 0, Y = 0 },
                                Extents = new DMLG.Extent() { ExtentLength = 0, ExtentWidth = 0 },
                                ChildOffset = new DMLG.Offset() { X = 0, Y = 0 },
                                ChildExtents = new DMLG.Extent() { ExtentLength = 0, ExtentWidth = 0 }
                            }
                        }
                    }
                };

                Shape shape1;
                slide.SlideData.ShapeTree.Shapes.Add(shape1 = new Shape()
                {
                    NonVisualProperties = new NonVisualShapeProperties()
                    {
                        NonVisualDrawingProperties = new DMLGP.NonVisualProperties()
                        {
                            Id = 2,
                            Name = "Title"
                        },
                        NonVisualShapeDrawingProperties = new DMLGP.NonVisualShapeDrawingProperties()
                        {
                            ShapeLocks = new DMLG.Locks.ShapeLocks() { DisallowGrouping = true }
                        },
                        NonVisualProperties = new NonVisualProperties()
                        {
                            Placeholder = new PlaceholderShape() { Type = ST_PlaceholderType.CenteredTitle }
                        }
                    },
                    Properties = new DMLGP.ShapeProperties(),
                    Body = new DMLTx.Body()
                    {
                        Properties = new DMLTx.Properties.BodyProperties(),
                        Styles = new DMLTx.Properties.TextListStyles()
                    }
                });

                DMLTx.Paragraph p1;
                shape1.Body.Paragraphs.Add(p1 = new DMLTx.Paragraph());
                p1.Elements.Add(new DMLTx.Run()
                {
                    Text = new DocumentFormat.OpenXml.Lite.Shared.SimpleText() { Value = "Slide title" }
                });

                Shape shape2;
                slide.SlideData.ShapeTree.Shapes.Add(shape2 = new Shape()
                {
                    NonVisualProperties = new NonVisualShapeProperties()
                    {
                        NonVisualDrawingProperties = new DMLGP.NonVisualProperties()
                        {
                            Id = 3,
                            Name = "Subtitle"
                        },
                        NonVisualShapeDrawingProperties = new DMLGP.NonVisualShapeDrawingProperties()
                        {
                            ShapeLocks = new DMLG.Locks.ShapeLocks() { DisallowGrouping = true }
                        },
                        NonVisualProperties = new NonVisualProperties()
                        {
                            Placeholder = new PlaceholderShape() { Type = ST_PlaceholderType.Subtitle, Index = 1 }
                        }
                    },
                    Properties = new DMLGP.ShapeProperties(),
                    Body = new DMLTx.Body()
                    {
                        Properties = new DMLTx.Properties.BodyProperties(),
                        Styles = new DMLTx.Properties.TextListStyles()
                    }
                });

                DMLTx.Paragraph p2;
                shape2.Body.Paragraphs.Add(p2 = new DMLTx.Paragraph());
                p2.Elements.Add(new DMLTx.Run()
                {
                    Text = new DocumentFormat.OpenXml.Lite.Shared.SimpleText() { Value = "Slide subtitle" }
                });

                slide.Override = new ColorSchemeMapOverride() { Override = new MasterColorMapping() };
            }

            #endregion

            #region Create Theme

            {
                theme.Elements = new DMLS.ThemeElements()
                {
                    ColorScheme = new DMLC.ColorScheme()
                    {
                        Name = "Sample",
                        Dark1 = new DMLC.Color { ColorElement = new DMLC.SystemColorModel { ColorValue = DMLT.ST_SystemColorVal.WindowTextSystemColor, LastColor = "000000" } },
                        Light1 = new DMLC.Color { ColorElement = new DMLC.SystemColorModel { ColorValue = DMLT.ST_SystemColorVal.WindowSystemColor, LastColor = "FFFFFF" } },
                        Dark2 = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "1F497D" } },
                        Light2 = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "EEECE1" } },
                        Accent1 = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "4F81BD" } },
                        Accent2 = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "C0504D" } },
                        Accent3 = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "9BBB59" } },
                        Accent4 = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "8064A2" } },
                        Accent5 = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "4BACC6" } },
                        Accent6 = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "F79646" } },
                        Hyperlink = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "0000FF" } },
                        FollowedHyperlink = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "800080" } }
                    },
                    FontScheme = new DMLF.FontScheme()
                    {
                        Name = "Sample",
                        MajorFont = new DMLF.FontElement()
                        {
                            Latin = new DMLF.Font() { Typeface = "Calibri" },
                            EastAsian = new DMLF.Font() { Typeface = "" },
                            ComplexScript = new DMLF.Font() { Typeface = "" }
                        },
                        MinorFont = new DMLF.FontElement()
                        {
                            Latin = new DMLF.Font() { Typeface = "Calibri" },
                            EastAsian = new DMLF.Font() { Typeface = "" },
                            ComplexScript = new DMLF.Font() { Typeface = "" }
                        }
                    },
                    FormatScheme = new DMLS.FormatScheme()
                    {
                        Name = "Sample"
                    }
                };

                theme.Elements.FormatScheme.LineStyles.Add(new DMLG.Line());
                theme.Elements.FormatScheme.LineStyles.Add(new DMLG.Line());
                theme.Elements.FormatScheme.LineStyles.Add(new DMLG.Line());

                theme.Elements.FormatScheme.FillStyles.Add(new DMLV.NoFill());
                theme.Elements.FormatScheme.FillStyles.Add(new DMLV.NoFill());
                theme.Elements.FormatScheme.FillStyles.Add(new DMLV.NoFill());

                theme.Elements.FormatScheme.BackgroundFillStyles.Add(new DMLV.NoFill());
                theme.Elements.FormatScheme.BackgroundFillStyles.Add(new DMLV.NoFill());
                theme.Elements.FormatScheme.BackgroundFillStyles.Add(new DMLV.NoFill());

                theme.Elements.FormatScheme.EffectStyles.Add(new DMLE.EffectStyle()
                {
                    EffectProperty = new DMLE.EffectDag()
                });
                theme.Elements.FormatScheme.EffectStyles.Add(new DMLE.EffectStyle()
                {
                    EffectProperty = new DMLE.EffectDag()
                });
                theme.Elements.FormatScheme.EffectStyles.Add(new DMLE.EffectStyle()
                {
                    EffectProperty = new DMLE.EffectDag()
                });
            }

            #endregion

            pres.Save(@"samples\shapes.pptx");

            P.Presentation parsed = null;
            if (DocumentManager.Instance.OpenPresentationDocument(@"samples\shapes.pptx", out parsed))
            {
                DMLS.Theme parsedTheme = parsed.FindThemes()[0];
                P.SlideLayout parsedLayout = parsed.FindLayoutForSlide(parsed.Slides[0]);
                Console.WriteLine("Sample created and parsed succesfully!");
            }
            Console.ReadLine();
        }
    }
}
